package com.situ.hotel.controller;

import java.io.IOException;
import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import com.situ.hotel.entity.Dept;
import com.situ.hotel.entity.Guest;
import com.situ.hotel.util.JDBCUtil;
import com.situ.hotel.util.JSONResult;
import com.situ.hotel.util.JSONUtil;

// 继承HttpServlet，Servlet和Jsp都是可以在浏览器直接访问
// http://localhost:8080/Hotel/guest
@WebServlet("/guest")
public class GuestServlet extends HttpServlet{

	// 访问Servlet默认就是执行Service方法
	@Override
	protected void service(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
		// syst
		System.out.println("GuestServlet.service()");
		//  http://localhost:8080/Hotel/guest?method=selectAll
		//  http://localhost:8080/Hotel/guest?method=delete&code=001
		String method = req.getParameter("method");
		// method没有值，默认让他查找所有
		if (method == null || "".equals(method)) {
			method = "selectAll";
		}
		switch (method) {
		case "selectAll":
			selectAll(req, resp);
			break;
		case "delete":
			delete(req, resp);
			break;
		case "getAddPage":
			getAddPage(req, resp);
			break;
		case "add":
			add(req, resp);
			break;

		default:
			break;
		}
		
		
		
	}

	private void add(HttpServletRequest req, HttpServletResponse resp) {
		String code = req.getParameter("code");
		String name = req.getParameter("name");
		String gender = req.getParameter("gender");
		String phone = req.getParameter("phone");
		String company = req.getParameter("company");
		
		
		Connection connection = null;
		PreparedStatement statement = null;
		int count = 0;
		try {
			connection = JDBCUtil.getConnection();
			String sql = "INSERT INTO `guest`(`code`,`name`,`gender`,`phone`,`company`) VALUES(?,?,?,?,?)";
			statement = connection.prepareStatement(sql);
			statement.setString(1, code);
			statement.setString(2, name);
			statement.setString(3, gender);
			statement.setString(4, phone);
			statement.setString(5, company);
			System.out.println(statement);
			count = statement.executeUpdate();
			System.out.println("count: " + count);
		} catch (SQLException e) {
			e.printStackTrace();
		}
		
		if (count == 1) {
			JSONResult jsonResult = JSONResult.success("添加成功");
			JSONUtil.obj2Json(jsonResult, resp);
		} else {
			JSONResult jsonResult = JSONResult.error("添加失败");
			JSONUtil.obj2Json(jsonResult, resp);
		}
	}

	// 为了转发到/guest/add.jsp，通过后台转发的目的是因为这个页面需要所有的部门的信息，
	// 所以可以在转发到这个页面之前查找出来，到add.jsp页面展示
	private void getAddPage(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
		req.getRequestDispatcher("/web/page/guest/add.jsp").forward(req, resp);
	}

	private void delete(HttpServletRequest req, HttpServletResponse resp) throws IOException {
		// http://localhost:8080/Hotel/guest?mehtod=delete&code=5
		String code = req.getParameter("code");
		
		Connection connection = null;
		PreparedStatement statement = null;
		int count = 0;
		try {
			connection = JDBCUtil.getConnection();
			String sql = "DELETE FROM `guest` WHERE `code`=?";
			statement = connection.prepareStatement(sql);
			// 填上？占位符的值
			statement.setString(1, code);
			// 在执行之前打印statement会打印执行的sql语句
			System.out.println(statement);
			count = statement.executeUpdate();
			System.out.println("count: " + count);
		} catch (SQLException e) {
			e.printStackTrace();
		}
		
		// 删除完了之后应该跳转到查找所有的界面，把最新的数据查找展示
		// resp.sendRedirect(req.getContextPath() + "/guest?method=selectAll");
		
		if (count == 1) {
			JSONResult jsonResult = JSONResult.success("删除成功");
			JSONUtil.obj2Json(jsonResult, resp);
		} else {
			JSONResult jsonResult = JSONResult.error("删除失败");
			JSONUtil.obj2Json(jsonResult, resp);
		}
	}

	private void selectAll(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
		String searchName = req.getParameter("name");
		if (searchName == null) {
			searchName = "";
		}
		
		Connection connection = null;
		PreparedStatement statement = null;
		ResultSet resultSet = null;
		ArrayList<Guest> list = new ArrayList<>();
		try {
			connection = JDBCUtil.getConnection();
			String sql = "SELECT `id`,`code`,`name`,`gender`,`company`,`phone` FROM `guest` WHERE `name` LIKE ? ";
			statement = connection.prepareStatement(sql);
			// "%" + searchName + "%" 豆豆加加
			statement.setString(1, "%" + searchName + "%");
			System.out.println(statement);
			resultSet = statement.executeQuery();
			while (resultSet.next()) {
				int id = resultSet.getInt("id");
				String code = resultSet.getString("code");
				String name = resultSet.getString("name");
				String gender = resultSet.getString("gender");
				String phone = resultSet.getString("phone");
				String company = resultSet.getString("company");
				Guest guest = new Guest(id, code, name, gender, company, phone);
				list.add(guest);
			}
			
			for (Guest guest : list) {
				System.out.println(guest);
			}
		} catch (SQLException e) {
			e.printStackTrace();
		} finally {
			JDBCUtil.close(connection, statement, resultSet);
		}
		
		// 使用LayUI之后需要返回JSON格式的数据
		JSONResult jsonResult = JSONResult.success(list);
		JSONUtil.obj2Json(jsonResult, resp);
		
	}
}
